// pages/channel/user/followers/followers.js
var qcloud = require('../../../../vendor/qcloud-weapp-client-sdk/index');

// 引入配置
var config = require('../../../../config');

// 显示繁忙提示
var showBusy = text => wx.showToast({
    title: text,
    icon: 'loading',
    duration: 10000
});

// 显示成功提示
var showSuccess = text => {
    wx.hideToast();
    wx.showToast({
        title: text,
        icon: 'success'
    });
};

// 显示失败提示
var showModel = (title, content) => {
    wx.hideToast();

    wx.showModal({
        title,
        content: JSON.stringify(content),
        showCancel: false
    });
};

var showMsg = (title, content) => {
    wx.hideToast();

    wx.showModal({
        title,
        content: content,
        showCancel: false
    });
};
//获取应用实例
var busy=false;
var app = getApp()
Page({
  data: {
    userInfo: {},
    loading:true,
    users:[],
    busy:false,
    loading_show:false,
    users:[]
  },
  user_list(){
    var self = this;
    if(self.data.busy){
      return;
    }
    self.setData({
        loading:true,
        busy:true
    });
    var url=config.service.apiUrl+'followers';
    qcloud.request({
        // 要请求的地址
        url: url,
        success(result) {
            if(result.data.success==true){
                self.setData({
                    users:result.data.followers.data
                })
            }
            if(result.data.followers.next_page_url!==null){
                self.setData({
                    next_data:true,
                    next_page_url:result.data.followers.next_page_url
                })
            }else{
                self.setData({
                    next_data:false,
                    next_page_url:null
                })
            }
            console.log('request success', result);
        },

        fail(error) {
            console.log('request fail', error);
        },

        complete() {
            self.setData({
                loading:false,
                busy:false
            })
            busy=false;
            
            wx.hideNavigationBarLoading();
            console.log('request complete');
        }
    });
  },
  load_more(){
      var self = this;
      var request_url=self.data.next_page_url;
      if(!request_url){
          return;
      }
      if(self.data.busy){
        return;
      }
      self.setData({
        loading:true,
        busy:true
      });
        qcloud.request({
            // 要请求的地址
            url: request_url,
            success(result) {
                if(result.data.success==true){
                    var item=self.data.users;
                    var obj=result.data.followers.data;
                    for (var i=0;i<obj.length;i++){
                        item.push(obj[i]);
                    }
                    self.setData({
                        users:item
                    });
                    if(result.data.followers.next_page_url!==null){
                        self.setData({
                            next_data:true,
                            next_page_url:result.data.followers.next_page_url
                        })
                    }else{
                        self.setData({
                            next_data:false,
                            next_page_url:null
                        })
                    }
                }
                console.log('request success', result);
            },

            fail(error) {
                console.log('request fail', error);
            },

            complete() {
                self.setData({
                    loading:false,
                    busy:false
                })
                console.log('request complete');
            }
    });
  },
  onReachBottom: function() {
    var self = this;
    self.load_more();
  },
  del:function(e){
    console.log(e);
    var follower_id=e.target.dataset.followerId;
    var self=this;
    if(busy||!follower_id){
        return;
    }
    if(wx.showLoading) {
        wx.showLoading({
            title: '操作中...',
            mask:true
        })
    }else{
        self.setData({
            loading_show:true,
        })
    }
    var data={};
    data.follower_id=follower_id;
    qcloud.request({
        // 要请求的地址
        url: config.service.apiUrl+'followers',
        method:"DELETE",
        data: data,
        success(result) {
            if(wx.hideLoading) {
                    wx.hideLoading();
            }else{
                self.setData({
                    loading_show:false,
                })
            }
            if(result.data.success==true){
                showSuccess('操作成功');
            }else{
                showMsg("提示信息","操作失败");
            }
            self.user_list();
            console.log('request success', result);
        },

        fail(error) {
            if(wx.hideLoading) {
                    wx.hideLoading();
            }else{
                self.setData({
                    loading_show:false,
                })
            }
            console.log('request fail', error);
        },

        complete() {
            console.log('request complete');
        }
     });
  },
  onLoad: function () {
    console.log('onLoad');
  },
  
  onShow:function(){
      if(busy){
        return;
      }
      busy=true;
      var self=this;
      app.getUserInfo(function(userInfo){
        wx.showNavigationBarLoading();
        self.user_list();
      });
  }
})